package com.pfzy.lib.net.interceptor;

import com.pfzy.lib.sdk.log.UXLog;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 配置请求头
 */
public class HttpHeaderInterceptor implements Interceptor {
    public final String TAG = HttpHeaderInterceptor.class.getSimpleName();
    private Map<String, String> mHeaderParamsMap = new HashMap<>();

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder requestBuilder = chain.request().newBuilder();

        // 添加公共参数,添加到header中
        if (mHeaderParamsMap.size() > 0) {
            for (Map.Entry<String, String> params : mHeaderParamsMap.entrySet()) {
                requestBuilder.header(params.getKey(), params.getValue());
            }
        }

        Request request = requestBuilder.build();
//        String accessToken = "token";
//        String tokenType = "tokenType";
//        Request request = chain.request().newBuilder()
//                .header("app_key", "appId")
//                .header("Authorization", tokenType + " " + accessToken)
//                .header("Content-Type", "application/json")
//                .addHeader("Connection", "close")
//                .addHeader("Accept-Encoding", "identity")
//                .build();
        return chain.proceed(request);
    }

    public void addHeaderParams(String key, String value) {
        UXLog.d(TAG, "key: " + key+"value: " + value);
        mHeaderParamsMap.put(key, value);
    }
}
